$(function() {
    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });

    $("#form-admin-add").validate({
        rules: {
            userName: {
                required: true,
                minlength: 2,
                maxlength: 10
            },
            password: {
                required: true,
                minlength: 3,
                maxlength: 16
            },
            password2: {
                required: true,
                minlength: 3,
                maxlength: 16,
                equalTo: "#password"
            },
            sex: {
                required: true,
            },
            // phoneNumber: {
            //     required: true,
            //     isPhone: true,
            // },
            // email: {
            //     required: true,
            //     email: true,
            // },
            roleId: {
                required: true,
            },
        },
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler:function(form){
            var userName = $("#userName").val();
            if($.trim(userName) == ""){
                layer.msg("请输入合法的用户名",{icon:2,time:4000});
                return;
            }
            $.ajax({
                type: "POST",
                url: '/admin/addAdmin',
                data: $('#form-admin-add').serialize(),
                success: function (data) {
                    var data = eval("(" + data + ")");
                    if (data.success == true) {
                        layer.msg(data.msg,{icon:1,time:2000},function(){
                            parent.location.reload();
                            //注意: 关闭打开的添加页 无法实现刷新 现修改为刷新父页面
                            // var index = parent.layer.getFrameIndex(window.name);
                            // parent.$('.btn-refresh').click();
                            // parent.layer.close(index);
                        });
                    } else if(data.success == false){
                        layer.msg(data.msg,{icon:2,time:4000});
                        return;
                    }
                }
            });
            //注意: 控件最初表单请求 未调试成功
            // $(form).ajaxSubmit({
            //     type: 'post',
            //     dataType: "json",
            //     url: "/admin/addAdmin.do" ,
            //     success: function(data){
            //         layer.msg('添加成功!',{icon:1,time:1000});
            //     },
            //     error: function(XmlHttpRequest, textStatus, errorThrown){
            //         layer.msg('error!',{icon:1,time:1000});
            //     }
            // });
            // var index = parent.layer.getFrameIndex(window.name);
            // parent.$('.btn-refresh').click();
            // parent.layer.close(index);
        }
    });
});